package com.dg11185.nearshop.baidumap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.android.nearshop.R;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.dg11185.nearshop.view.SlidingUpPanelLayout;
import com.dg11185.nearshop.view.SlidingUpPanelLayout.PanelSlideListener;

/**
 * 地图显示导航路径界面
 * 
 * @author：Daibo
 * @time：2014-5-11 下午3:09:09
 */
public class ShopRouteActivity extends BaseMapActivity {

    /** 从Intent中获取参数的值 */
    private int routePosition;

    private int trafficState;

    private String content, distance;

    /** 抽屉面板相关变量 */
    private ImageView routeArrow;

    private TextView routeTitle, routeDis;

    private ListView listView;

    private SlidingUpPanelLayout slidingLayout;

    private SimpleAdapter listAdapter;

    private List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        routePosition = intent.getIntExtra("route_position", 0);
        trafficState = intent.getIntExtra("traffic_state", 0);
        content = intent.getStringExtra("route_content");
        distance = intent.getStringExtra("route_distance");

        slidingLayout = (SlidingUpPanelLayout) this.findViewById(R.id.sliding_layout);
        slidingLayout.setPanelSlideListener(listener);
        routeArrow = (ImageView) this.findViewById(R.id.route_arrow);
        routeTitle = (TextView) this.findViewById(R.id.route_content);
        routeDis = (TextView) this.findViewById(R.id.route_distance);
        routeTitle.setText(content);
        routeDis.setText(distance);
        listView = (ListView) this.findViewById(R.id.map_route_detail_list);

        initStEnPoi();
        initListData();
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.map_shop_route);
    }

    /**
     * 在地图上显示路线图层
     */
    private void initStEnPoi() {

        if (trafficState == 0) {
            MyMapRoute.getInstance().showTransitRouteOverlay(mBaiduMap, routePosition);
        } else if (trafficState == 1) {
            MyMapRoute.getInstance().showDrivingRouteOverlay(mBaiduMap, routePosition);
        } else {
            MyMapRoute.getInstance().showWalkingRouteOverlay(mBaiduMap, routePosition);
        }
    }

    /**
     * 初始化listview的数据
     */
    private void initListData() {
        if (trafficState == 0) { // 公交

            TransitRouteResult result = MyMapRoute.getInstance().getTransitRouteResult();
            int steps = result.getRouteLines().get(routePosition).getAllStep().size();

            for (int a = 0; a < steps; a++) {
                Map<String, Object> map = new HashMap<String, Object>();
                String content = result.getRouteLines().get(routePosition).getAllStep().get(a).getInstructions();
                map.put("CONTENT", content);

                if (a == 0) {
                    if (content.contains("步行")) {
                        map.put("PICTURE", R.drawable.route_walking_st);
                    } else {
                        map.put("PICTURE", R.drawable.route_transit_st);
                    }
                } else if (a == steps - 1) {
                    if (content.contains("步行")) {
                        map.put("PICTURE", R.drawable.route_walking_en);
                    } else {
                        map.put("PICTURE", R.drawable.route_transit_en);
                    }
                } else {
                    if (content.contains("步行")) {
                        map.put("PICTURE", R.drawable.route_walking);
                    } else {
                        map.put("PICTURE", R.drawable.route_transit);
                    }
                }
                listData.add(map);
            }
            listAdapter = new SimpleAdapter(this, listData, R.layout.map_shop_route_list_item1, new String[] {
                    "PICTURE", "CONTENT" }, new int[] { R.id.route_pic, R.id.route_content });
            listView.setAdapter(listAdapter);

        } else if (trafficState == 1) {// 驾车
            DrivingRouteResult result = MyMapRoute.getInstance().getDrivingRouteResult();
            int steps = result.getRouteLines().get(routePosition).getAllStep().size();

            for (int a = 0; a < steps; a++) {
                Map<String, Object> map = new HashMap<String, Object>();
                if (a == 0) {
                    map.put("PICTURE", R.drawable.route_driving_st);
                } else if (a == steps - 1) {
                    map.put("PICTURE", R.drawable.route_driving_en);
                } else {
                    map.put("PICTURE", R.drawable.route_driving);
                }
                map.put("CONTENT", result.getRouteLines().get(routePosition).getAllStep().get(a).getInstructions());
                listData.add(map);
            }
            listAdapter = new SimpleAdapter(this, listData, R.layout.map_shop_route_list_item1, new String[] {
                    "PICTURE", "CONTENT" }, new int[] { R.id.route_pic, R.id.route_content });
            listView.setAdapter(listAdapter);
        } else {// 步行
            WalkingRouteResult result = MyMapRoute.getInstance().getWalkingRouteResult();
            int steps = result.getRouteLines().get(routePosition).getAllStep().size();

            for (int a = 0; a < steps; a++) {
                Map<String, Object> map = new HashMap<String, Object>();
                if (a == 0) {
                    map.put("PICTURE", R.drawable.route_walking_st);
                } else if (a == steps - 1) {
                    map.put("PICTURE", R.drawable.route_walking_en);
                } else {
                    map.put("PICTURE", R.drawable.route_walking);
                }
                map.put("CONTENT", result.getRouteLines().get(routePosition).getAllStep().get(a).getInstructions());
                listData.add(map);
            }
            listAdapter = new SimpleAdapter(this, listData, R.layout.map_shop_route_list_item1, new String[] {
                    "PICTURE", "CONTENT" }, new int[] { R.id.route_pic, R.id.route_content });
            listView.setAdapter(listAdapter);
        }
    }

    /**
     * slidepanel状态改变时候的回调函数
     */
    private PanelSlideListener listener = new PanelSlideListener() {

        @Override
        public void onPanelCollapsed(View panel) {
            routeArrow.setImageResource(R.drawable.ic_red_arrow_up);

        }

        @Override
        public void onPanelExpanded(View panel) {
            routeArrow.setImageResource(R.drawable.ic_red_arrow_down);
        }

        @Override
        public void onPanelAnchored(View panel) {
            routeArrow.setImageResource(R.drawable.ic_red_arrow_up);

        }

        @Override
        public void onPanelSlide(View panel, float slideOffset) {
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
